#!/bin/bash

INPUT_INFILE=$1
OUTPUT_FILE=$2
#echo $OUTPUT_FILE
for DIR in $1 #"ubenchflit-"$1 #flit-64 flit-128 flit-256 flit-512 "
do
 for FILE in $DIR/*.txt
 do
    #echo $FILE
    NUM_INSTR=$(grep "Million" $FILE | tail -n 1 | cut -d" " -f4) #>> $OUTPUT_FILE
    IPC=$(grep "fp:" $FILE | tail -n 1) #>> $OUTPUT_FILE
    LAT=$(grep "Avg. Message Latency" $FILE | tail -n 1 | cut -d":" -f2) #>> $OUTPUT_FILE
    POW_NET=$(grep "Avg. Power consumption" $FILE | tail -n 1 | cut -d":" -f2) #>> $OUTPUT_FILE
    MLP=$(grep avg_slack $FILE | tail -n 64 | cut -d":" -f 13 | cut -d" " -f1 | awk '{sum+=$1} END { print "MLP:", sum/NR}')
    SLACK=$(grep avg_slack $FILE | tail -n 64 | cut -d":" -f 14 | cut -d" " -f1 | awk '{sum1+=$1} END { print "SLACK:", sum1/NR}')
    AVG_COP=$(grep avg_slack $FILE | cut -d":" -f 15 | cut -d" " -f1 | awk '{sum2+=$1} END { print "AVG_COP:", sum2/NR}')
    AVG_COMP=$(grep avg_slack $FILE | cut -d":" -f 16 | cut -d" " -f1 | awk '{sum3+=$1} END { print "AVG_COMP:", sum3/NR}')
    NUM_NET_PH=$(grep avg_slack $FILE | tail -n 64 | cut -d":" -f 17 | cut -d" " -f1 | awk '{sum4+=$1} END { print "NUM_NET_PH:", sum4/NR}')
    TOT_NET_LN=$(grep avg_slack $FILE | tail -n 64 | cut -d":" -f 18 | cut -d" " -f1 | awk '{sum5+=$1} END { print "TOT_NET_LN:", sum5/NR}')
    TOT_NET_HT=$(grep avg_slack $FILE | tail -n 64 | cut -d":" -f 19 | cut -d" " -f1 | awk '{sum6+=$1} END { print "TOT_NET_HT:", sum6/NR}')
    TOT_COMP_LN=$(grep avg_slack $FILE | tail -n 64 | cut -d":" -f 20 | cut -d" " -f1 | awk '{sum7+=$1} END { print "TOT_COMP_LN:", sum7/NR}')
    TOT_AVG_HT_PER_NET_LN=$(grep avg_slack $FILE | tail -n 64 | cut -d":" -f 21 | cut -d" " -f1 | awk '{sum8+=$1} END { print "TOT_AVG_HT_PER_NET_LN:", sum8/NR}')
    RUNNING_AVG_HT_PER_NET_LN=$(grep avg_slack $FILE | tail -n 64 | cut -d":" -f 23 | cut -d" " -f1 | awk '{sum9+=$1} END { print "RUNNING_AVG_HT_PER_NET_LN:", sum9/NR}')
    TOTAL_EPISODE_LENGTH=$(grep avg_slack $FILE | tail -n 64 | cut -d":" -f 25 | cut -d" " -f1 | awk '{sum9+=$1} END { print "TOTAL_EPISODE_LENGTH:", sum9/NR}')
    RUNNING_AVG_EPISODE_HEIGHT=$(grep avg_slack $FILE | tail -n 64 | cut -d":" -f 26 | cut -d" " -f1 | awk '{sum9+=$1} END { print "RUNNING_AVG_EPISODE_HEIGHT:", sum9/NR}')
    echo $FILE $NUM_INSTR $IPC $LAT $POW_NET $MLP $SLACK $AVG_COP $AVG_COMP $NUM_NET_PH $TOT_NET_LN $TOT_NET_HT $TOT_COMP_LN $TOT_AVG_HT_PER_NET_LN $RUNNING_AVG_HT_PER_NET_LN $TOTAL_EPISODE_LENGTH $RUNNING_AVG_EPISODE_HEIGHT >> $OUTPUT_FILE
 done
done
